package com.luo.chengrui.module.sa.service.datapermission;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import com.luo.chengrui.module.sa.controller.admin.datapermission.vo.DataPermissionConfigCreateReqVO;
import com.luo.chengrui.module.sa.controller.admin.datapermission.vo.DataPermissionConfigExportReqVO;
import com.luo.chengrui.module.sa.controller.admin.datapermission.vo.DataPermissionConfigPageReqVO;
import com.luo.chengrui.module.sa.controller.admin.datapermission.vo.DataPermissionConfigUpdateReqVO;
import com.luo.chengrui.module.sa.convert.datapermission.DataPermissionConfigConvert;
import com.luo.chengrui.module.sa.dal.dataobject.datapermission.DataPermissionConfigDO;
import com.luo.chengrui.module.sa.dal.dataobject.datapermission.DataPermissionConfigViewDO;
import com.luo.chengrui.module.sa.dal.mysql.datapermission.DataPermissionConfigMapper;
import com.luo.chengrui.module.sa.service.datapermission.DataPermissionConfigExcludeService;
import com.luo.chengrui.module.sa.service.datapermission.DataPermissionConfigService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.luo.chengrui.module.sa.enums.ErrorCodeConstants.DATA_PERMISSION_CONFIG_NOT_EXISTS;

/**
 * 数据权限配置 Service 实现类
 *
 * @author 诚睿
 */
@Service
@Validated
public class DataPermissionConfigServiceImpl implements DataPermissionConfigService {

    @Resource
    private DataPermissionConfigMapper dataPermissionConfigMapper;
    @Resource
    private DataPermissionConfigExcludeService dataPermissionConfigExcludeService;

    @Override
    public String createDataPermissionConfig(DataPermissionConfigCreateReqVO createReqVO) {
        // 插入
        DataPermissionConfigDO dataPermissionConfig = DataPermissionConfigConvert.INSTANCE.convert(createReqVO);
        dataPermissionConfigMapper.insert(dataPermissionConfig);
        // 返回
        return dataPermissionConfig.getId();
    }

    @Override
    public void updateDataPermissionConfig(DataPermissionConfigUpdateReqVO updateReqVO) {
        // 校验存在
        validateDataPermissionConfigExists(updateReqVO.getId());
        // 更新
        DataPermissionConfigDO updateObj = DataPermissionConfigConvert.INSTANCE.convert(updateReqVO);
        dataPermissionConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteDataPermissionConfig(String id) {
        // 校验存在
        validateDataPermissionConfigExists(id);
        // 删除
        dataPermissionConfigMapper.deleteById(id);
        dataPermissionConfigExcludeService.deleteByConfigId(id);
    }

    private void validateDataPermissionConfigExists(String id) {
        if (dataPermissionConfigMapper.selectById(id) == null) {
            throw exception(DATA_PERMISSION_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public DataPermissionConfigDO getDataPermissionConfig(String id) {
        return dataPermissionConfigMapper.selectById(id);
    }

    @Override
    public List<DataPermissionConfigDO> getDataPermissionConfigList(Collection<String> ids) {
        return dataPermissionConfigMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DataPermissionConfigDO> getDataPermissionConfigPage(DataPermissionConfigPageReqVO pageReqVO) {
        return dataPermissionConfigMapper.selectPage(pageReqVO);
    }


    @Override
    public PageResult<DataPermissionConfigViewDO> getDataPermissionConfigPageCustomize(DataPermissionConfigPageReqVO pageVO) {
        IPage<T> mpPage = MyBatisUtils.buildPage(pageVO);
        IPage<DataPermissionConfigViewDO>  page = dataPermissionConfigMapper.selectPageCustomize(mpPage,pageVO);
        // 转换返回
        return new PageResult(mpPage.getRecords(), mpPage.getTotal());
    }

    @Override
    public List<DataPermissionConfigDO> getDataPermissionConfigList(DataPermissionConfigExportReqVO exportReqVO) {
        return dataPermissionConfigMapper.selectList(exportReqVO);
    }
}

